package com.lgmshare.component.widget.recyclerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Recycler Adapter基类
 *
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2016/5/19 18:07
 */
public abstract class RecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected final String TAG = RecyclerViewAdapter.this.getClass().getSimpleName();

    protected Context mContext;
    protected LayoutInflater mLayoutInflater;
    protected List<T> mDataList;

    protected OnItemClickListener mOnItemClickListener;
    protected OnItemLongClickListener mOnItemLongClickListener;
    protected OnItemChildClickListener mChildClickListener;

    public RecyclerViewAdapter(Context context) {
        this(context, null);
    }

    /**
     * Same as RecyclerViewAdapter(Context,int) but with
     * some initialization data.
     *
     * @param context The context.
     * @param data    A new list is created out of this one to avoid mutable list
     */
    public RecyclerViewAdapter(Context context, List<T> data) {
        this.mDataList = (data == null ? new ArrayList<>() : new ArrayList<>(data));
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    /**
     * setting up a new instance to dataList;
     *
     * @param data
     */
    public void setList(List<T> data) {
        this.mDataList.clear();
        if (data != null) {
            this.mDataList.addAll(data);
        }
        this.notifyDataSetChanged();
    }

    /**
     * additional data;
     * 方法内已实现notify更新
     *
     * @param data
     */
    public void addList(List<T> data) {
        if (data != null) {
            this.mDataList.addAll(data);
        }
        this.notifyDataSetChanged();
    }

    public List<T> getList() {
        return this.mDataList;
    }

    /**
     * 方法内已实现notify更新
     *
     * @param item
     */
    public void addItem(T item) {
        if (item != null) {
            this.mDataList.add(item);
        }
        this.notifyItemInserted(mDataList.size());
    }

    /**
     * 方法内已实现notify更新
     *
     * @param position
     * @param item
     */
    public void addItem(int position, T item) {
        if (item != null) {
            this.mDataList.add(position, item);
        }
        this.notifyItemInserted(position);
        //必须要重新刷新一遍数据，纠正position
        this.notifyItemRangeChanged(position, mDataList.size() - position);
    }

    /**
     * 方法内已实现notify更新
     *
     * @param position
     */
    public void removeItem(int position) {
        this.mDataList.remove(position);
        this.notifyItemRemoved(position);
        //必须要重新刷新一遍数据，纠正position
        this.notifyItemRangeChanged(position, mDataList.size() - position);
    }

    /**
     * 方法内已实现notify更新
     *
     * @param t
     */
    public void removeItem(T t) {
        int position = mDataList.indexOf(t);
        if (position >= 0) {
            this.mDataList.remove(t);
            this.notifyItemRemoved(position);
            //必须要重新刷新一遍数据，纠正position
            this.notifyItemRangeChanged(position, mDataList.size() - position);
        }
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    public T getItem(int position) {
        return this.mDataList.get(position);
    }

    public int getDataCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public int getItemCount() {
        return this.mDataList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //判断当前item类型
    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    /**
     * 获取xml资源文件view, 直接使用view可重载此方法
     *
     * @param layoutResId
     * @param parent
     * @return item布局view
     */
    protected View getItemView(int layoutResId, ViewGroup parent) {
        return mLayoutInflater.inflate(layoutResId, parent, false);
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = getItemView(onBindItemViewResId(), parent);
        RecyclerViewHolder viewHolder = new RecyclerViewHolder(mContext, itemView);
        bindItemViewClickListener(itemView, viewHolder);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
        recyclerViewHolder.setDataPosition(position);
        onBindItemViewData((RecyclerViewHolder) holder, getItem(position));
    }

    /**
     * Note that if you've called {@link RecyclerView.Adapter#notifyDataSetChanged()}, until the
     * next layout pass, the return value of this method will be {@link #NO_POSITION}.
     */
    protected void bindItemViewClickListener(View view, final RecyclerViewHolder finalViewHolder) {
        if (mOnItemClickListener != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = finalViewHolder.getAdapterPosition();
                    if (adapterPosition < 0) {
                        return;
                    }
                    mOnItemClickListener.onItemClick(v, adapterPosition);
                }
            });
        }
        if (mOnItemLongClickListener != null) {
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int adapterPosition = finalViewHolder.getAdapterPosition();
                    if (adapterPosition < 0) {
                        return true;
                    }
                    //TODO 长按操作，如果返回false，onItemClick事件将会响应，返回true，则不会。
                    return mOnItemLongClickListener.onItemLongClick(v, adapterPosition);
                }
            });
        }
    }

    /**
     * Note that if you've called {@link RecyclerView.Adapter#notifyDataSetChanged()}, until the
     * next layout pass, the return value of this method will be {@link #NO_POSITION}.
     */
    protected void bindItemChildClickListener(View view, final RecyclerViewHolder finalViewHolder) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = finalViewHolder.getAdapterPosition();
                if (adapterPosition < 0) {
                    return;
                }
                if (mChildClickListener != null) {
                    mChildClickListener.onItemChildClick(v, adapterPosition);
                }
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.mOnItemLongClickListener = onItemLongClickListener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener childClickListener) {
        this.mChildClickListener = childClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        boolean onItemLongClick(View view, int position);
    }

    public interface OnItemChildClickListener {
        void onItemChildClick(View view, int position);
    }

    protected abstract int onBindItemViewResId();

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param holder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    protected abstract void onBindItemViewData(RecyclerViewHolder holder, T item);

}
